package com.health.trans.pojo;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;

import javax.persistence.Column;
import javax.persistence.Table;
import java.lang.reflect.Field;

/**
 * @author nqlz
 */
public class SqlSentence {
    /**
     * 生成插入语句
     *
     * @param clazz 与数据库中字段一一对应的类
     * @param <T>   数据实体类型 如 User
     */
    public static <T> String getInsertSql(Class<T> clazz) {
        //insert into table_name (column_name1,column_name2, ...) values (value1,value2, ...)
        Table table = clazz.getAnnotation(Table.class);
        //insert into platform_standard.tb_base_register (register_type, treatment_no, hosp_code, card_type, card_num) values (?, ?, ?, ?, ?)
        String tableName = table.catalog() + "." + table.name();
        String sql = "";
        Field[] fields = ReflectUtil.getFieldsDirectly(clazz, false);
        StringBuffer topHalf = new StringBuffer("insert into " + tableName + " (");
        StringBuffer afterAalf = new StringBuffer("values (");
        for (Field field : fields) {
            Column column = field.getAnnotation(Column.class);
            topHalf.append(column.name() + ",");
            //afterAalf.append(":" + column.name() + ",");
            //topHalf.append(field.getName() + ",");
            afterAalf.append(":" + field.getName() + ",");
            //afterAalf.append("?" + ",");
        }
        topHalf = new StringBuffer(StrUtil.removeSuffix(topHalf.toString(), ","));
        afterAalf = new StringBuffer(StrUtil.removeSuffix(afterAalf.toString(), ","));
        topHalf.append(") ");
        afterAalf.append(") ");
        sql = topHalf.toString() + afterAalf.toString();
        return sql;
    }


    static final String QUERY_CHAR = "SELECT * FROM ";
    static final String COUNT_CHAR = "SELECT  count(1) FROM ";

    public static <T> String getSelectSql(Class<T> tClass) {
        return sqlBase(QUERY_CHAR, tClass, "", "");
    }

    public static <T> String getSelectSql(Class<T> tClass, String otherCondition) {
        return getSelectSql(tClass) + otherCondition;
    }

    private static <T> String sqlBase(String type, Class<T> tClass, String baseName, String otherCondition) {
        Table table = tClass.getAnnotation(Table.class);
        String catalog = table.catalog();
        boolean isCatlog = StrUtil.isNotBlank(catalog);
        boolean hasBase = StrUtil.isNotBlank(baseName);
        String catalogFinal = baseName + ".";
        //如果有数据库名，则校验和实体类是否相对，以免index或其他库
        if (isCatlog) {
            catalogFinal = catalog + ".";
        } else if (!hasBase) {
            catalogFinal = "";
        }
        return type + catalogFinal + table.name() + " " + otherCondition;
    }

    public static String getTableName(Class<?> clazz) {
        return sqlBase("", clazz, "", "");
    }

    public static String countQuery(Class<?> tClass) {
        return sqlBase(COUNT_CHAR, tClass, "", "");
    }

    public static String countQuery(Class<?> tClass, String otherCondition) {
        return countQuery(tClass) + otherCondition;
    }

}
